{{template "admin/layout/header_full.html" .}}
<div class="layui-card-body">
    <form class="layui-form" action="">
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <input class="layui-input" placeholder="菜单名称" name="name">
        </div>
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <select name="level">
                <option value="">菜单等级</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
        </div>
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                <i class="layui-icon">&#xe615;</i>搜索
            </button>
        </div>
    </form>
    <hr>
    <button class="layui-btn" onclick="xadmin.open('添加菜单','/admin/menu/create')">
        <i class="layui-icon"></i>添加菜单
    </button>
    <table id="menu" lay-filter="menu"></table>
</div>

<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;

        //初始化表格
        table.render({
            elem: '#menu',
            url: '/admin/menu/lists',
            page: true,
            cols: [[
                // {field: 'id', title: 'ID'},
                {field: 'name', title: '菜单名称'},
                {field: 'level', title: '菜单等级'},
                {
                    field: 'top', title: '上级菜单', templet(d) {
                        return d.top.name ? d.top.name + "(ID:" + d.top.id + ")" : ""
                    }
                },
                {field: 'url', title: 'Url', style: 'color: red;'},
                {
                    field: 'created_at', title: '创建时间', templet(d) {
                        return GoTimeFormat(new Date(d.created_at))
                    }
                },
                {
                    field: 'updated_at', title: '修改时间', templet: function (d) {
                        return GoTimeFormat(new Date(d.updated_at))
                    }
                },
                {field: 'experience', title: '操作', toolbar: '#button', width: 150},
            ]],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.lists
                };
            }
        });

        //监听表格事件
        table.on('tool(menu)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    $.post("/admin/menu/delete/" + data.id, {}, function (result) {
                        layer.msg(result.message);
                        if (result.code === 0) {
                            obj.del();
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                xadmin.open('编辑菜单', '/admin/menu/update/' + data.id)
            }
        });

        //搜索
        form.on('submit(sreach)', function (data) {
            table.reload('menu', {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
    })
</script>

<script type="text/html" id="button">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{{template "admin/layout/footer_full.html" .}}